<?php
/**
 * @package App
 * @subpackage resource
 */
 
/**
 *
 * Google API リソース
 * 
 * http://code.google.com/intl/ja/more/#products-featured-android
 *
 * @package     App
 * @subpackage  resource
 * @author      $Author: moriwaki $
 * @version     $Id: Google.php 360 2008-06-29 01:58:40Z moriwaki $
 * 
 */
class Google extends BEAR_Vo
{
 
    /**
     * API uri
     */
    private static $_uri = "";

	/**
	 * google api key
	 */
	private static $googleApiKey = '';
	
    /**
     * 作成
     * 
     * @param array $values
     * + name: string 名前
     * @return integer id or error
     */
    public function onCreate($values)
    {
    }
 
    /**
     * 変更
     * 
     * @param array $values
     * + id: integer $id
     * + name: string 名前
     * @return integer id or error
     */
    public function onUpdate($values)
    {
    }
 
    /**
     * 読み込み
     * 
     * @param array $values
     * + mode: strign $mode
     * @return array
     */
    public function onRead($values)
    {
        //assert & mapping
        self::assert(isset($values['mode']));
        $mode = $values['mode'];
	    $option = array(
	    "timeout" => "3", // タイムアウトの秒数指定
	    );
	
        switch($mode){
	    case 'codesearch' :
	        $url = 'http://www.google.com/codesearch/feeds/search';

	        $params['q'] = App_Values::getValue('q', $values);

	        $p = new App_Url($url,$params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

	        break;

	    case 'videosearch' :
	        $url = 'http://gdata.youtube.com/feeds/videos';

	        $params['vq'] = App_Values::getValue('q', $values);
	        $params['start-index'] = App_Values::getValue('start-index', $values);
	        $params['max-results'] = App_Values::getValue('max-results', $values);
	        $params['orderby'] = App_Values::getValue('orderby', $values);
	        $params['alt'] = 'atom';

	        $p = new App_Url($url, $params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

	        break;

	    case 'videodetail' :

	        $url = 'http://gdata.youtube.com/feeds/videos/%s';
	        $id = App_Values::getValue('id', $values);

	        $url = sprintf($url, $id);

	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

	        $parser = new XML_Unserializer(array('parseAttributes' => true));
	        $parser->unserialize($result);
	        $result = $parser->getUnserializedData();

	        break;

	    case 'videorated' :
	        $url = 'http://gdata.youtube.com/feeds/standardfeeds/top_rated';

	        $params['start-index'] = App_Values::getValue('start-index', $values);
	        $params['max-results'] = App_Values::getValue('max-results', $values);
	        $params['time'] = App_Values::getValue('time', $values, 'all_time');

	        $p = new App_Url($url, $params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

	        break;

	    case 'videofavorites' :
	        $url = 'http://gdata.youtube.com/feeds/standardfeeds/top_favorites';

	        $params['start-index'] = App_Values::getValue('start-index', $values);
	        $params['max-results'] = App_Values::getValue('max-results', $values);
	        $params['time'] = App_Values::getValue('time', $values, 'all_time');

	        $p = new App_Url($url, $params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

	        break;

	    case 'videoviewed' :
	        $url = 'http://gdata.youtube.com/feeds/standardfeeds/most_viewed';

	        $params['start-index'] = App_Values::getValue('start-index', $values);
	        $params['max-results'] = App_Values::getValue('max-results', $values);
	        $params['time'] = App_Values::getValue('time', $values, 'all_time');

	        $p = new App_Url($url, $params);
	        $url = $p->getUrl();
	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

	        break;

	    case 'videomobile' :
	        $url = 'http://gdata.youtube.com/feeds/standardfeeds/watch_on_mobile';

	        $params['start-index'] = App_Values::getValue('start-index', $values);
	        $params['max-results'] = App_Values::getValue('max-results', $values);
	        $params['time'] = App_Values::getValue('time', $values, 'all_time');

	        $p = new App_Url($url, $params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

	        break;

	    case 'video_category' :
	        $url = 'http://gdata.youtube.com/feeds/videos/-/';

	        $category = getValue($values, 'category');
	        $url .= $category;

	        $params['start-index'] = App_Values::getValue('start-index', $values);
	        $params['max-results'] = App_Values::getValue('max-results', $values);
	        $params['orderby'] = App_Values::getValue('orderby', $values);
	        //$params['time'] = App_Values::getValue($values, 'time', 'all_time');
	        $params['alt'] = 'atom';

	        $p = new App_Url($url, $params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        $req->sendRequest();
	        $result = $req->getResponseBody();

            $parser = new XML_Unserializer(array('parseAttributes' => true));
            $parser->unserialize($result);
            $result = $parser->getUnserializedData();

	        break;

	    case 'trans_en2ja' :
	        $url = 'http://ajax.googleapis.com/ajax/services/language/translate';

	        $params['v'] = '1.0';
	        $params['q'] = App_Values::getValue('q', $values);
	        $params['langpair'] = App_Values::getValue('language_pair', $values,'en|ja');
	        $p = new App_Url($url, $params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        $req->setHttpVer("1.1");
	        $req->addHeader('User-Agent',  'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');

	        $req->sendRequest();
	        $result = $req->getResponseBody();
	        $result = json_decode($result, true);
	        break;

	    case 'trans_ja2en' :
	        $url = 'http://ajax.googleapis.com/ajax/services/language/translate';

	        $params['v'] = '1.0';
	        $params['q'] = App_Values::getValue($values, 'q');
	        $params['langpair'] = App_Values::getValue('language_pair', $values, 'ja|en');
	        $p = new App_Url($url, $params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        $req->setHttpVer("1.1");
	        $req->addHeader('User-Agent',  'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
	        $req->sendRequest();
	        $result = $req->getResponseBody();
	        $result = json_decode($result, true);
		}

        return $result;
    }
 
    /**
     * 削除
     * 
     * @param array $values
     * + id: integer id
     * @return MDB2_Result
     */
    public function onDelete($values)
    {
    }
}
